{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>674</td>\n",
       "      <td>125</td>\n",
       "      <td>84250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>夏季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>384</td>\n",
       "      <td>125</td>\n",
       "      <td>48000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>841</td>\n",
       "      <td>125</td>\n",
       "      <td>105125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价     销售额\n",
       "0  MP3   李可  春季  上海  541  125   67625\n",
       "1  MP3   李可  秋季  青岛  674  125   84250\n",
       "2  MP3   李亮  春季  上海  720  125   90000\n",
       "3  MP3   李亮  夏季  上海  641  125   80125\n",
       "4  MP3   张平  春季  上海  721  125   90125\n",
       "5  MP3   张平  夏季  青岛  384  125   48000\n",
       "6  MP3  周顺利  夏季  上海  354  125   44250\n",
       "7  MP3  周顺利  秋季  青岛  841  125  105125"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "mydf1 = pd.read_excel('myexcel1.xls',sheet_name=1)\n",
    "display(mydf1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002501B66D910>\n"
     ]
    }
   ],
   "source": [
    "myg1 = mydf1.groupby('城市')\n",
    "print(myg1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'上海': [0, 2, 3, 4, 6], '青岛': [1, 5, 7]}\n"
     ]
    }
   ],
   "source": [
    "print(myg1.groups)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价    销售额\n",
       "0  MP3   李可  春季  上海  541  125  67625\n",
       "2  MP3   李亮  春季  上海  720  125  90000\n",
       "3  MP3   李亮  夏季  上海  641  125  80125\n",
       "4  MP3   张平  春季  上海  721  125  90125\n",
       "6  MP3  周顺利  夏季  上海  354  125  44250"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(myg1.get_group('上海'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'上海'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价    销售额\n",
       "0  MP3   李可  春季  上海  541  125  67625\n",
       "2  MP3   李亮  春季  上海  720  125  90000\n",
       "3  MP3   李亮  夏季  上海  641  125  80125\n",
       "4  MP3   张平  春季  上海  721  125  90125\n",
       "6  MP3  周顺利  夏季  上海  354  125  44250"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'青岛'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>674</td>\n",
       "      <td>125</td>\n",
       "      <td>84250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>夏季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>384</td>\n",
       "      <td>125</td>\n",
       "      <td>48000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>841</td>\n",
       "      <td>125</td>\n",
       "      <td>105125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价     销售额\n",
       "1  MP3   李可  秋季  青岛  674  125   84250\n",
       "5  MP3   张平  夏季  青岛  384  125   48000\n",
       "7  MP3  周顺利  秋季  青岛  841  125  105125"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for name,group in myg1:\n",
    "    display(name)\n",
    "    display(group)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('上海', '夏季'): [3, 6], ('上海', '春季'): [0, 2, 4], ('青岛', '夏季'): [5], ('青岛', '秋季'): [1, 7]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myg2 = mydf1.groupby(['城市','时间'])\n",
    "myg2.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('上海', '夏季')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价    销售额\n",
       "3  MP3   李亮  夏季  上海  641  125  80125\n",
       "6  MP3  周顺利  夏季  上海  354  125  44250"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "('上海', '春季')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品 业务员  时间  城市   数量   单价    销售额\n",
       "0  MP3  李可  春季  上海  541  125  67625\n",
       "2  MP3  李亮  春季  上海  720  125  90000\n",
       "4  MP3  张平  春季  上海  721  125  90125"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "('青岛', '夏季')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>夏季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>384</td>\n",
       "      <td>125</td>\n",
       "      <td>48000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品 业务员  时间  城市   数量   单价    销售额\n",
       "5  MP3  张平  夏季  青岛  384  125  48000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "('青岛', '秋季')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>674</td>\n",
       "      <td>125</td>\n",
       "      <td>84250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>841</td>\n",
       "      <td>125</td>\n",
       "      <td>105125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价     销售额\n",
       "1  MP3   李可  秋季  青岛  674  125   84250\n",
       "7  MP3  周顺利  秋季  青岛  841  125  105125"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for name,group in myg2:\n",
    "    display(name)\n",
    "    display(group)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价    销售额\n",
       "0  MP3   李可  春季  上海  541  125  67625\n",
       "2  MP3   李亮  春季  上海  720  125  90000\n",
       "3  MP3   李亮  夏季  上海  641  125  80125\n",
       "4  MP3   张平  春季  上海  721  125  90125\n",
       "6  MP3  周顺利  夏季  上海  354  125  44250"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "city_mygb = mydf1.groupby('城市')\n",
    "display(city_mygb.get_group('上海'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>上海</th>\n",
       "      <td>125.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>125.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     mean\n",
       "城市       \n",
       "上海  125.0\n",
       "青岛  125.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(city_mygb['单价'].agg(['mean']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>sum</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>上海</th>\n",
       "      <td>74425.0</td>\n",
       "      <td>372125</td>\n",
       "      <td>90125</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>79125.0</td>\n",
       "      <td>237375</td>\n",
       "      <td>105125</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       mean     sum     max  count\n",
       "城市                                \n",
       "上海  74425.0  372125   90125      5\n",
       "青岛  79125.0  237375  105125      3"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(city_mygb['销售额'].agg(['mean','sum','max','count']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('夏季', '周顺利'): [6], ('夏季', '张平'): [5], ('夏季', '李亮'): [3], ('春季', '张平'): [4], ('春季', '李亮'): [2], ('春季', '李可'): [0], ('秋季', '周顺利'): [7], ('秋季', '李可'): [1]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time_yewu = mydf1.groupby(['时间','业务员'])\n",
    "time_yewu.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>sum</th>\n",
       "      <th>max</th>\n",
       "      <th>min</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>时间</th>\n",
       "      <th>业务员</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">夏季</th>\n",
       "      <th>周顺利</th>\n",
       "      <td>354.0</td>\n",
       "      <td>354</td>\n",
       "      <td>354</td>\n",
       "      <td>354</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张平</th>\n",
       "      <td>384.0</td>\n",
       "      <td>384</td>\n",
       "      <td>384</td>\n",
       "      <td>384</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李亮</th>\n",
       "      <td>641.0</td>\n",
       "      <td>641</td>\n",
       "      <td>641</td>\n",
       "      <td>641</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">春季</th>\n",
       "      <th>张平</th>\n",
       "      <td>721.0</td>\n",
       "      <td>721</td>\n",
       "      <td>721</td>\n",
       "      <td>721</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李亮</th>\n",
       "      <td>720.0</td>\n",
       "      <td>720</td>\n",
       "      <td>720</td>\n",
       "      <td>720</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李可</th>\n",
       "      <td>541.0</td>\n",
       "      <td>541</td>\n",
       "      <td>541</td>\n",
       "      <td>541</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">秋季</th>\n",
       "      <th>周顺利</th>\n",
       "      <td>841.0</td>\n",
       "      <td>841</td>\n",
       "      <td>841</td>\n",
       "      <td>841</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李可</th>\n",
       "      <td>674.0</td>\n",
       "      <td>674</td>\n",
       "      <td>674</td>\n",
       "      <td>674</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         mean  sum  max  min  count\n",
       "时间 业务员                             \n",
       "夏季 周顺利  354.0  354  354  354      1\n",
       "   张平   384.0  384  384  384      1\n",
       "   李亮   641.0  641  641  641      1\n",
       "春季 张平   721.0  721  721  721      1\n",
       "   李亮   720.0  720  720  720      1\n",
       "   李可   541.0  541  541  541      1\n",
       "秋季 周顺利  841.0  841  841  841      1\n",
       "   李可   674.0  674  674  674      1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(time_yewu['数量'].agg(['mean','sum','max','min','count']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "      <th>不同城市数量平均值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "      <td>595.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>674</td>\n",
       "      <td>125</td>\n",
       "      <td>84250</td>\n",
       "      <td>633.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "      <td>595.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "      <td>595.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "      <td>595.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>夏季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>384</td>\n",
       "      <td>125</td>\n",
       "      <td>48000</td>\n",
       "      <td>633.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "      <td>595.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>841</td>\n",
       "      <td>125</td>\n",
       "      <td>105125</td>\n",
       "      <td>633.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价     销售额  不同城市数量平均值\n",
       "0  MP3   李可  春季  上海  541  125   67625      595.4\n",
       "1  MP3   李可  秋季  青岛  674  125   84250      633.0\n",
       "2  MP3   李亮  春季  上海  720  125   90000      595.4\n",
       "3  MP3   李亮  夏季  上海  641  125   80125      595.4\n",
       "4  MP3   张平  春季  上海  721  125   90125      595.4\n",
       "5  MP3   张平  夏季  青岛  384  125   48000      633.0\n",
       "6  MP3  周顺利  夏季  上海  354  125   44250      595.4\n",
       "7  MP3  周顺利  秋季  青岛  841  125  105125      633.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mydf1['不同城市数量平均值'] = mydf1.groupby('城市')['数量'].transform('mean')\n",
    "display(mydf1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mydf1['不同城市销售额平均值'] = mydf1.groupby('城市')['销售额'].transform('mean')\n",
    "mydf1['不同城市销售额最大值'] = mydf1.groupby('城市')['销售额'].transform('max')\n",
    "mydf1['不同城市和时间的平均值'] = mydf1.groupby(['城市','时间'])['数量'].transform('mean')\n",
    "display(mydf1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mydf1.loc[:,['城市','时间','不同城市数量平均值','不同城市和时间的平均值']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电器产品</th>\n",
       "      <th>业务员</th>\n",
       "      <th>时间</th>\n",
       "      <th>城市</th>\n",
       "      <th>数量</th>\n",
       "      <th>单价</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>541</td>\n",
       "      <td>125</td>\n",
       "      <td>67625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李可</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>674</td>\n",
       "      <td>125</td>\n",
       "      <td>84250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>720</td>\n",
       "      <td>125</td>\n",
       "      <td>90000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MP3</td>\n",
       "      <td>李亮</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>641</td>\n",
       "      <td>125</td>\n",
       "      <td>80125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>春季</td>\n",
       "      <td>上海</td>\n",
       "      <td>721</td>\n",
       "      <td>125</td>\n",
       "      <td>90125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MP3</td>\n",
       "      <td>张平</td>\n",
       "      <td>夏季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>384</td>\n",
       "      <td>125</td>\n",
       "      <td>48000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>夏季</td>\n",
       "      <td>上海</td>\n",
       "      <td>354</td>\n",
       "      <td>125</td>\n",
       "      <td>44250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>MP3</td>\n",
       "      <td>周顺利</td>\n",
       "      <td>秋季</td>\n",
       "      <td>青岛</td>\n",
       "      <td>841</td>\n",
       "      <td>125</td>\n",
       "      <td>105125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  电器产品  业务员  时间  城市   数量   单价     销售额\n",
       "0  MP3   李可  春季  上海  541  125   67625\n",
       "1  MP3   李可  秋季  青岛  674  125   84250\n",
       "2  MP3   李亮  春季  上海  720  125   90000\n",
       "3  MP3   李亮  夏季  上海  641  125   80125\n",
       "4  MP3   张平  春季  上海  721  125   90125\n",
       "5  MP3   张平  夏季  青岛  384  125   48000\n",
       "6  MP3  周顺利  夏季  上海  354  125   44250\n",
       "7  MP3  周顺利  秋季  青岛  841  125  105125"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "pivot_df = pd.read_excel('myexcel1.xls',sheet_name=1)\n",
    "display(pivot_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "      <th>数量</th>\n",
       "      <th>销售额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>上海</th>\n",
       "      <td>125.0</td>\n",
       "      <td>595.4</td>\n",
       "      <td>74425.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>125.0</td>\n",
       "      <td>633.0</td>\n",
       "      <td>79125.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       单价     数量      销售额\n",
       "城市                       \n",
       "上海  125.0  595.4  74425.0\n",
       "青岛  125.0  633.0  79125.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(pivot_df,index='城市',values=['数量','单价','销售额'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function sum at 0x000002501A770180> is currently using DataFrameGroupBy.sum. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"sum\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function sum at 0x000002501A770180> is currently using DataFrameGroupBy.sum. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"sum\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function sum at 0x000002501A770180> is currently using DataFrameGroupBy.sum. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"sum\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function mean at 0x000002501A771260> is currently using DataFrameGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function mean at 0x000002501A771260> is currently using DataFrameGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function mean at 0x000002501A771260> is currently using DataFrameGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function max at 0x000002501A770860> is currently using DataFrameGroupBy.max. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"max\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function max at 0x000002501A770860> is currently using DataFrameGroupBy.max. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"max\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function max at 0x000002501A770860> is currently using DataFrameGroupBy.max. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"max\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function min at 0x000002501A7709A0> is currently using DataFrameGroupBy.min. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"min\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function min at 0x000002501A7709A0> is currently using DataFrameGroupBy.min. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"min\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\2062082884.py:1: FutureWarning: The provided callable <function min at 0x000002501A7709A0> is currently using DataFrameGroupBy.min. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"min\" instead.\n",
      "  display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">mean</th>\n",
       "      <th colspan=\"3\" halign=\"left\">max</th>\n",
       "      <th colspan=\"3\" halign=\"left\">min</th>\n",
       "      <th colspan=\"3\" halign=\"left\">size</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">销售额</th>\n",
       "      <th colspan=\"3\" halign=\"left\">销售额</th>\n",
       "      <th colspan=\"3\" halign=\"left\">销售额</th>\n",
       "      <th colspan=\"3\" halign=\"left\">销售额</th>\n",
       "      <th colspan=\"3\" halign=\"left\">销售额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th>上海</th>\n",
       "      <th>青岛</th>\n",
       "      <th>总计</th>\n",
       "      <th>上海</th>\n",
       "      <th>青岛</th>\n",
       "      <th>总计</th>\n",
       "      <th>上海</th>\n",
       "      <th>青岛</th>\n",
       "      <th>总计</th>\n",
       "      <th>上海</th>\n",
       "      <th>青岛</th>\n",
       "      <th>总计</th>\n",
       "      <th>上海</th>\n",
       "      <th>青岛</th>\n",
       "      <th>总计</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>时间</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>夏季</th>\n",
       "      <td>124375</td>\n",
       "      <td>48000</td>\n",
       "      <td>172375</td>\n",
       "      <td>62187.500000</td>\n",
       "      <td>48000.0</td>\n",
       "      <td>57458.333333</td>\n",
       "      <td>80125</td>\n",
       "      <td>48000</td>\n",
       "      <td>80125</td>\n",
       "      <td>44250</td>\n",
       "      <td>48000</td>\n",
       "      <td>44250</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>春季</th>\n",
       "      <td>247750</td>\n",
       "      <td>0</td>\n",
       "      <td>247750</td>\n",
       "      <td>82583.333333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>82583.333333</td>\n",
       "      <td>90125</td>\n",
       "      <td>0</td>\n",
       "      <td>90125</td>\n",
       "      <td>67625</td>\n",
       "      <td>0</td>\n",
       "      <td>67625</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>秋季</th>\n",
       "      <td>0</td>\n",
       "      <td>189375</td>\n",
       "      <td>189375</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>94687.5</td>\n",
       "      <td>94687.500000</td>\n",
       "      <td>0</td>\n",
       "      <td>105125</td>\n",
       "      <td>105125</td>\n",
       "      <td>0</td>\n",
       "      <td>84250</td>\n",
       "      <td>84250</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>总计</th>\n",
       "      <td>372125</td>\n",
       "      <td>237375</td>\n",
       "      <td>609500</td>\n",
       "      <td>74425.000000</td>\n",
       "      <td>79125.0</td>\n",
       "      <td>76187.500000</td>\n",
       "      <td>90125</td>\n",
       "      <td>105125</td>\n",
       "      <td>105125</td>\n",
       "      <td>44250</td>\n",
       "      <td>48000</td>\n",
       "      <td>44250</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       sum                          mean                           max  \\\n",
       "       销售额                           销售额                           销售额   \n",
       "城市      上海      青岛      总计            上海       青岛            总计     上海   \n",
       "时间                                                                       \n",
       "夏季  124375   48000  172375  62187.500000  48000.0  57458.333333  80125   \n",
       "春季  247750       0  247750  82583.333333      0.0  82583.333333  90125   \n",
       "秋季       0  189375  189375      0.000000  94687.5  94687.500000      0   \n",
       "总计  372125  237375  609500  74425.000000  79125.0  76187.500000  90125   \n",
       "\n",
       "                      min               size        \n",
       "                      销售额                销售额        \n",
       "城市      青岛      总计     上海     青岛     总计   上海 青岛 总计  \n",
       "时间                                                  \n",
       "夏季   48000   80125  44250  48000  44250    2  1  3  \n",
       "春季       0   90125  67625      0  67625    3  0  3  \n",
       "秋季  105125  105125      0  84250  84250    0  2  2  \n",
       "总计  105125  105125  44250  48000  44250    5  3  8  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(pd.pivot_table(pivot_df,index='时间',columns='城市',values=['销售额'],aggfunc=[np.sum,np.mean,np.max,np.min,np.size],fill_value=0,margins=True,margins_name='总计'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>时间</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "      <th>总计</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>上海</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>总计</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "时间  夏季  春季  秋季  总计\n",
       "城市                \n",
       "上海   2   3   0   5\n",
       "青岛   1   0   2   3\n",
       "总计   3   3   2   8"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(pivot_df.城市,pivot_df.时间,margins=True,margins_name='总计')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\4145262822.py:1: FutureWarning: The provided callable <function sum at 0x000002501A770180> is currently using DataFrameGroupBy.sum. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"sum\" instead.\n",
      "  pd.crosstab(pivot_df.城市,pivot_df.时间,values=pivot_df.销售额,aggfunc=[np.sum,np.mean,np.max,np.min,np.size])\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\4145262822.py:1: FutureWarning: The provided callable <function mean at 0x000002501A771260> is currently using DataFrameGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  pd.crosstab(pivot_df.城市,pivot_df.时间,values=pivot_df.销售额,aggfunc=[np.sum,np.mean,np.max,np.min,np.size])\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\4145262822.py:1: FutureWarning: The provided callable <function max at 0x000002501A770860> is currently using DataFrameGroupBy.max. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"max\" instead.\n",
      "  pd.crosstab(pivot_df.城市,pivot_df.时间,values=pivot_df.销售额,aggfunc=[np.sum,np.mean,np.max,np.min,np.size])\n",
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\4145262822.py:1: FutureWarning: The provided callable <function min at 0x000002501A7709A0> is currently using DataFrameGroupBy.min. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"min\" instead.\n",
      "  pd.crosstab(pivot_df.城市,pivot_df.时间,values=pivot_df.销售额,aggfunc=[np.sum,np.mean,np.max,np.min,np.size])\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">mean</th>\n",
       "      <th colspan=\"3\" halign=\"left\">max</th>\n",
       "      <th colspan=\"3\" halign=\"left\">min</th>\n",
       "      <th colspan=\"3\" halign=\"left\">size</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>时间</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>上海</th>\n",
       "      <td>124375.0</td>\n",
       "      <td>247750.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>62187.5</td>\n",
       "      <td>82583.333333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>80125.0</td>\n",
       "      <td>90125.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>44250.0</td>\n",
       "      <td>67625.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>48000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>189375.0</td>\n",
       "      <td>48000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>94687.5</td>\n",
       "      <td>48000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>105125.0</td>\n",
       "      <td>48000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84250.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         sum                         mean                             max  \\\n",
       "时间        夏季        春季        秋季       夏季            春季       秋季       夏季   \n",
       "城市                                                                          \n",
       "上海  124375.0  247750.0       NaN  62187.5  82583.333333      NaN  80125.0   \n",
       "青岛   48000.0       NaN  189375.0  48000.0           NaN  94687.5  48000.0   \n",
       "\n",
       "                           min                   size            \n",
       "时间       春季        秋季       夏季       春季       秋季   夏季   春季   秋季  \n",
       "城市                                                               \n",
       "上海  90125.0       NaN  44250.0  67625.0      NaN  2.0  3.0  NaN  \n",
       "青岛      NaN  105125.0  48000.0      NaN  84250.0  1.0  NaN  2.0  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(pivot_df.城市,pivot_df.时间,values=pivot_df.销售额,aggfunc=[np.sum,np.mean,np.max,np.min,np.size])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\18027\\AppData\\Local\\Temp\\ipykernel_15308\\157467855.py:1: FutureWarning: The provided callable <function mean at 0x000002501A771260> is currently using DataFrameGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  pd.crosstab(pivot_df.城市,[pivot_df.时间,pivot_df.业务员],values=pivot_df.销售额,aggfunc=np.mean)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>时间</th>\n",
       "      <th colspan=\"3\" halign=\"left\">夏季</th>\n",
       "      <th colspan=\"3\" halign=\"left\">春季</th>\n",
       "      <th colspan=\"2\" halign=\"left\">秋季</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>业务员</th>\n",
       "      <th>周顺利</th>\n",
       "      <th>张平</th>\n",
       "      <th>李亮</th>\n",
       "      <th>张平</th>\n",
       "      <th>李亮</th>\n",
       "      <th>李可</th>\n",
       "      <th>周顺利</th>\n",
       "      <th>李可</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>城市</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>上海</th>\n",
       "      <td>44250.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>80125.0</td>\n",
       "      <td>90125.0</td>\n",
       "      <td>90000.0</td>\n",
       "      <td>67625.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>NaN</td>\n",
       "      <td>48000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>105125.0</td>\n",
       "      <td>84250.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "时间        夏季                         春季                          秋季         \n",
       "业务员      周顺利       张平       李亮       张平       李亮       李可       周顺利       李可\n",
       "城市                                                                          \n",
       "上海   44250.0      NaN  80125.0  90125.0  90000.0  67625.0       NaN      NaN\n",
       "青岛       NaN  48000.0      NaN      NaN      NaN      NaN  105125.0  84250.0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(pivot_df.城市,[pivot_df.时间,pivot_df.业务员],values=pivot_df.销售额,aggfunc=np.mean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>时间</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>业务员</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>周顺利</th>\n",
       "      <td>0.500</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张平</th>\n",
       "      <td>0.500</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李亮</th>\n",
       "      <td>0.500</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李可</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>总计</th>\n",
       "      <td>0.375</td>\n",
       "      <td>0.375</td>\n",
       "      <td>0.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "时间      夏季     春季    秋季\n",
       "业务员                    \n",
       "周顺利  0.500  0.000  0.50\n",
       "张平   0.500  0.500  0.00\n",
       "李亮   0.500  0.500  0.00\n",
       "李可   0.000  0.500  0.50\n",
       "总计   0.375  0.375  0.25"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(pivot_df.业务员,pivot_df.时间,margins=True,margins_name='总计',normalize='index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>时间</th>\n",
       "      <th>夏季</th>\n",
       "      <th>春季</th>\n",
       "      <th>秋季</th>\n",
       "      <th>总计</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>业务员</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>周顺利</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张平</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李亮</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李可</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "时间         夏季        春季   秋季    总计\n",
       "业务员                               \n",
       "周顺利  0.333333  0.000000  0.5  0.25\n",
       "张平   0.333333  0.333333  0.0  0.25\n",
       "李亮   0.333333  0.333333  0.0  0.25\n",
       "李可   0.000000  0.333333  0.5  0.25"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(pivot_df.业务员,pivot_df.时间,margins=True,margins_name='总计',normalize='columns')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
